/*
 *	2004 JAN 24	SHP	
 *		1. Added comments for code
 *		2. Added license to class
 */

using System;
using System.Collections;
using System.Collections.Specialized;
using FrameworkQ.ObjectSpace.Data.Schema;

namespace FrameworkQ.ObjectSpace
{
	/// <summary>
	/// A class that caches the CRUD+more commands
	/// </summary>
	internal class ActionCache 
	{
		private Hashtable _actions = Hashtable.Synchronized(new Hashtable());

		public ActionCache()
		{
		}

		/// <summary>
		/// Adds a action command object to the Cache
		/// </summary>
		/// <param name="actionCommand">Action Command object</param>
		public void Add (ActionCommand  actionCommand)
		{	
			lock(_actions)
			{
				if (!_actions.ContainsKey (actionCommand.Action))
				{
					// TODO : Thorw our own exception here, do not wait 
					// for freamework to generate one.
					_actions.Add (actionCommand.Action,actionCommand);
				}
				else
				{
					// Success
					_actions.Add (actionCommand.Action,actionCommand);
				}
			}
		}

		/// <summary>
		/// Checks if the action command is in the cache
		/// </summary>
		/// <param name="actionCommand">The command to look for</param>
		/// <returns>True if already cached</returns>
		public bool Contains (ActionCommand actionCommand)
		{
			return Contains(actionCommand.Action);
		}


		/// <summary>
		/// Checks if the action command is in the cache
		/// </summary>
		/// <param name="action">The action key to look for</param>
		/// <returns>True if already cached</returns>
		public bool Contains ( string action)
		{

			return _actions.ContainsKey(action);
		}

		/// <summary>
		/// Get and action command that is in cache
		/// </summary>
		/// <param name="action">Key to get the action</param>
		/// <returns></returns>
		public ActionCommand  Get (string action)
		{
			return ((ActionCommand)_actions[action]);
		}


		/// <summary>
		/// Default property
		/// </summary>
		public ActionCommand this[ string action ]
		{
			get
			{
				return this.Get(action);
			}
		}

		/// <summary>
		/// Removes a action from cache
		/// </summary>
		/// <param name="action">The key of the action(action string)</param>
		public void Remove (string action)
		{
			lock(_actions)
			{
				_actions.Remove(action);
			}
		}
	}
}
